package com.kasa.gps.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.kasa.gps.db.table.LocationInfoTableAdapter;
import com.kasa.gps.db.table.VehiclesInfoTableAdapter;

/**
 * 订单GPS系统的数据库
 * */
public class OrderGPSDBAdapter {

	private static final String TAG = OrderGPSDBAdapter.class.getName();
	
	/** 中文编码 */
	public static final String ENCODING = "UTF-8";
	private static final String DATABASE_NAME="ordergps.db";
	private static final int DATABASE_VERSION = 1;
	
	//public class OrderGPSDBHelper extends SDCardSQLiteOpenHelper{
	public class OrderGPSDBHelper extends SQLiteOpenHelper{
//		public OrderGPSDBHelper(Context context){
//			this(context, "/kasa/data/",DATABASE_NAME, null, DATABASE_VERSION);
//		}
//		
//		
//		public OrderGPSDBHelper(Context context, String dbDir, String name,
//				CursorFactory factory, int version) {
//			super(context, dbDir, name, factory, version);
//			
//		}
		
		public OrderGPSDBHelper(Context context){
			this(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public OrderGPSDBHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			//
			String vehicles_sql ="CREATE TABLE " + VehiclesInfoTableAdapter.TABLE_NAME + "("+VehiclesInfoTableAdapter.ID + " INTEGER primary key autoincrement,"
												 											+VehiclesInfoTableAdapter.LICENSE_PLATES + " text not null,"
												 											+VehiclesInfoTableAdapter.TRANSPORT_PLANS + " text not null,"
												 											+VehiclesInfoTableAdapter.EXPECTED_TIME + " text not null,"
												 											+VehiclesInfoTableAdapter.LOCATION_UPDATE_FREQUENCY + " text not null,"
												 											+VehiclesInfoTableAdapter.LOCATION_UPDATE_DISTANCE + " text not null,"
												 											+VehiclesInfoTableAdapter.UPLOAD_SERVER + " text not null,"
												 											+VehiclesInfoTableAdapter.DATA_UPLOAD_FREQUENCY + " text not null"
												 											+");";
			db.execSQL(vehicles_sql);
			Log.i(TAG, vehicles_sql);
			
			//
			String location_sql ="CREATE TABLE " + LocationInfoTableAdapter.TABLE_NAME + "("+LocationInfoTableAdapter.ID + " INTEGER primary key autoincrement,"
																							+LocationInfoTableAdapter.VEHICLESINFO_ID + " INTEGER not null,"
																							+LocationInfoTableAdapter.LONGITUDE + " DOUBLE not null,"
																							+LocationInfoTableAdapter.LATITUDE + " DOUBLE not null,"
																							+LocationInfoTableAdapter.AREA + " text not null,"
																							+LocationInfoTableAdapter.STREE + " text not null,"
																							+LocationInfoTableAdapter.UPDATETIME + " text not null"
																							+");";
			db.execSQL(location_sql);
			Log.i(TAG, location_sql);
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			//
			db.execSQL("DROP TABLE IF EXISTS "+VehiclesInfoTableAdapter.TABLE_NAME +";");
			Log.i(TAG, VehiclesInfoTableAdapter.TABLE_NAME + " table version update from "+oldVersion+" to " +newVersion);
			//
			db.execSQL("DROP TABLE IF EXISTS "+LocationInfoTableAdapter.TABLE_NAME +";");
			Log.i(TAG, LocationInfoTableAdapter.TABLE_NAME + " table version update from "+oldVersion+" to " +newVersion);
		}
		
	}
}
